package com.tenny.bookstore.service.impl;

import com.tenny.bookstore.bean.Book;
import com.tenny.bookstore.bean.Category;
import com.tenny.bookstore.commons.Page;
import com.tenny.bookstore.dao.BookDao;
import com.tenny.bookstore.dao.CategoryDao;
import com.tenny.bookstore.dao.impl.BookDaoImpl;
import com.tenny.bookstore.dao.impl.CategoryDaoImpl;
import com.tenny.bookstore.service.BusinessService;

import java.util.List;
import java.util.UUID;


public class BusinessServiceImpl implements BusinessService {
	private CategoryDao categoryDao = new CategoryDaoImpl();
	private BookDao bookDao = new BookDaoImpl();
	public void addCategory(Category category) {
		category.setId(UUID.randomUUID().toString());
		categoryDao.save(category);
	}

	public List<Category> findAllCategories() {
		return categoryDao.findAll();
	}

	public Category findCategoryById(String categoryId) {
		return categoryDao.findById(categoryId);
	}

	public void addBook(Book book) {
		book.setId(UUID.randomUUID().toString());
		bookDao.save(book);
	}

	public Page findAllBookPageRecords(String pagenum) {
		
		int currentPageNum = 1;
		if(pagenum!=null){
			currentPageNum = Integer.parseInt(pagenum);
		}
		int totalRecords = bookDao.findAllBooksNumber();
		Page page = new Page(currentPageNum, totalRecords);
		
		page.setRecords(bookDao.findPageBooks(page.getStartIndex(), page.getPageSize()));
		
		return page;
	}

	public Book findBookById(String bookId) {

		return (Book) bookDao.findById(bookId);
	}

}
